//
//  NSNotiViewController.m
//  Findif
//
//  Created by Mac on 2018/12/10.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "NSNotiViewController.h"
#import "NSNotiModel.h"
#import "NotiTableViewCell.h"
#import "NotiDetailsViewController.h"

@interface NSNotiViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <NSNotiModel *> * notiArray;
@property (nonatomic, weak)UILabel * showLabel;

@end

@implementation NSNotiViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"系统消息";
  
    [self setupUI];
}

- (void)loadData{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    
    [[ZRNetworkTool sharedTool] getUserNoticeWithUser_id:user_id ResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.notiArray = [NSArray yy_modelArrayWithClass:[NSNotiModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setNotiArray:(NSArray<NSNotiModel *> *)notiArray{
    
    _notiArray = notiArray;
    
    [self.tableView reloadData];
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
        tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[NotiTableViewCell class] forCellReuseIdentifier:@"NotiTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"亲，还没有任何消息哦~";
    showLabel.textColor = ZRBlackColor;
    showLabel.textAlignment = NSTextAlignmentCenter;
    showLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
}

- (void)viewDidLayoutSubviews{
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.notiArray.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    return self.notiArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NotiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotiTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.notiArray[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSNotiModel * model = self.notiArray[indexPath.row];
    
    HiddenBar;
    
    NotiDetailsViewController * detailVC = [NotiDetailsViewController new];
    
    detailVC.ID = model.id;
    detailVC.time = model.inputtime;
    detailVC.content = model.notice_content;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

@end
